//
//  JTTopicTableViewCell.m
//  百思不得姐
//
//  Created by Robin Zhang on 2017/1/10.
//  Copyright © 2017年 BeiJingWangDianJieTong. All rights reserved.
//

#import "JTTopicTableViewCell.h"
#import "JTTopic.h"
#import <UIImageView+WebCache.h>
#import "JTComment.h"
#import "JTUser.h"
#import "JTTopicPictureView.h"
#import "JTTopicVideoView.h"
#import "JTTopicVoiceView.h"

@interface JTTopicTableViewCell ()

@property (weak, nonatomic) IBOutlet UIImageView *profileImageView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *createdAtLabel;
@property (weak, nonatomic) IBOutlet UILabel *text_label;
@property (weak, nonatomic) IBOutlet UIButton *dingButton;
@property (weak, nonatomic) IBOutlet UIButton *caiButton;
@property (weak, nonatomic) IBOutlet UIButton *repostButton;
@property (weak, nonatomic) IBOutlet UIButton *commentButton;
/** 最热评论-整体 */
@property (weak, nonatomic) IBOutlet UIView *topCmtView;
@property (weak, nonatomic) IBOutlet UILabel *topCmtContentLabel;

/* 中间控件 */
/** 图片控件 */
@property (nonatomic, weak) JTTopicPictureView *pictureView;
/** 声音控件 */
@property (nonatomic, weak) JTTopicVoiceView *voiceView;
/** 视频控件 */
@property (nonatomic, weak) JTTopicVideoView *videoView;

@end

@implementation JTTopicTableViewCell
#pragma mark - 懒加载
- (JTTopicPictureView *)pictureView {
    
    if (!_pictureView) {
        JTTopicPictureView *pictureView = [JTTopicPictureView jt_viewWithXib];
        [self.contentView addSubview:pictureView];
        _pictureView = pictureView;
    }
    return _pictureView;
}

- (JTTopicVoiceView *)voiceView {
    
    if (!_voiceView) {
        
        JTTopicVoiceView *voiceView = [JTTopicVoiceView jt_viewWithXib];
        [self.contentView addSubview:voiceView];
        _voiceView = voiceView;
    }
    return _voiceView;
}

- (JTTopicVideoView *)videoView {
    
    if (!_videoView) {
        
        JTTopicVideoView *videoView = [JTTopicVideoView jt_viewWithXib];
        [self.contentView addSubview:videoView];
        _videoView = videoView;
    }
    return _videoView;
}

#pragma mark - 初始化
- (void)awakeFromNib {
    [super awakeFromNib];
    
    [self setBackgroundView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"mainCellBackground"]]];
    
//    [self.topCmtView.layer setBorderColor:[UIColor orangeColor].CGColor];
//    [self.topCmtView.layer setBorderWidth:1.0f];
//    [self.topCmtView.layer setCornerRadius:5.0f];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)setTopic:(JTTopic *)topic {
    
    _topic = topic;
    
    /** 取消之前加载的图片并开始加载新图片 */
    [self.profileImageView sd_cancelCurrentImageLoad];
//    [self.profileImageView sd_cancelCurrentAnimationImagesLoad];
    
    [self.profileImageView setHeader:topic.profile_image];
    [self.nameLabel setText:topic.name];
    [self.createdAtLabel setText:topic.created_at]; // 调用getter方法[topic created_at]，在getter方法中计算时间
    [self.text_label setText:topic.text];
    
    /** 系统适配 */
    NSCalendar *calendar = nil;
    if ([NSCalendar respondsToSelector:@selector(calendarWithIdentifier:)]) {
        
        calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
    }
    else {
        calendar = [NSCalendar currentCalendar];
    }
    
    if ([NSCalendar respondsToSelector:@selector(calendarWithIdentifier:)]) {
        
        calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
    }
    else {
        calendar = [NSCalendar currentCalendar];
    }
    
    [self setupButtonL:self.dingButton number:topic.ding placeholder:@"顶"];
    [self setupButtonL:self.caiButton number:topic.cai placeholder:@"踩"];
    [self setupButtonL:self.repostButton number:topic.repost placeholder:@"分享"];
    [self setupButtonL:self.commentButton number:topic.comment placeholder:@"评论"];

    // 最热评论
    if (topic.top_cmt) { // 有最热评论
        [self.topCmtView setHidden:NO];

        NSString *userName = topic.top_cmt.user.username; // 用户名
        NSString *content = topic.top_cmt.content; // 评论内容
        if (self.topic.top_cmt.voiceuri.length) {
            
            content = @"[语音评论]";
        }
        
        [self.topCmtContentLabel setText:[NSString stringWithFormat:@"%@:%@", userName, content]];
    }
    else { // 没有最热评论
        
        [self.topCmtView setHidden:YES];
    }
    
    // 中间内容
    switch (topic.type) {
        case JTTopicTypeVideo: // 视频
            self.videoView.hidden = NO;
            self.videoView.frame = topic.contentFrame;
            self.videoView.topic = topic;
            
            self.voiceView.hidden = YES;
            self.pictureView.hidden = YES;
            break;
            
        case JTTopicTypeVoice: // 音频
            self.voiceView.hidden = NO;
            self.voiceView.frame = topic.contentFrame;
            self.voiceView.topic = topic;
            
            self.videoView.hidden = YES;
            self.pictureView.hidden = YES;
            break;
            
        case JTTopicTypeWord: // 段子
            self.videoView.hidden = YES;
            self.voiceView.hidden = YES;
            self.pictureView.hidden = YES;
            break;
        
        case JTTopicTypePicutre: // 图片
            self.pictureView.hidden = NO;
            self.pictureView.frame = topic.contentFrame;
            self.pictureView.topic = topic;
            
            self.videoView.hidden = YES;
            self.voiceView.hidden = YES;
            break;
            
        default:
            break;
    }
}

/**
 设置按钮的文字(placeholder是中文参数，故意留到最后，前面的参数就可以使用点语法智能提示)
 */
- (void)setupButtonL:(UIButton *)button number:(NSInteger)number placeholder:(NSString *)placeholder {
    
    if (number >= 10000) {
        [button setTitle:[NSString stringWithFormat:@"%.1f万", number / 10000.0] forState:UIControlStateNormal];
    }
    else if (number > 0) {
        [button setTitle:[NSString stringWithFormat:@"%ld", number] forState:UIControlStateNormal];
    }
    else {
        [button setTitle:placeholder forState:UIControlStateNormal];
    }
}

/**
 重写这个方法的目的：
 能够拦截所有设置cell的frame的操作
 */
- (void)setFrame:(CGRect)frame {
    
    frame.origin.y += JTMargin;
    frame.size.height -= JTMargin;
    
    [super setFrame:frame];
}

- (IBAction)more:(UIButton *)sender {
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    [alertController addAction:[UIAlertAction actionWithTitle:@"收藏" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }]];
    
    [alertController addAction:[UIAlertAction actionWithTitle:@"举报" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        
    }]];
    
    [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
    
    [self.window.rootViewController presentViewController:alertController animated:YES completion:nil];
}

@end
